<?php

function setCurrentItemTypeName($currentItemTypeName) {
    $GLOBALS["currentItemTypeName"] = $currentItemTypeName;
}

function getCurrentItemTypeName() {
    if (isset($GLOBALS["currentItemTypeName"])) {
        return $GLOBALS["currentItemTypeName"];
    }
    return null;
}

function webDateToDBDate($webDate) {
    $webDate = explode("/", $webDate);
    if (isset($webDate[0]) && isset($webDate[1]) && isset($webDate[2])) {
        return $webDate[2] . '-' . $webDate[0] . '-' . $webDate[1];
    }
    return "";
}

function dbDateTowebDate($dbDate) {
    $dbDate = explode(" ", $dbDate);
    $dbDate = explode("-", $dbDate[0]);
    if (isset($dbDate[0]) && isset($dbDate[1]) && isset($dbDate[2])) {
        return ((int) $dbDate[2]) . '.' . Settings::$month[(int) $dbDate[1]];
    }
    return "";
}

function dbDateToCalendarDate($dbDate) {
    $dbDate = explode(" ", $dbDate);
    $dbDate = explode("-", $dbDate[0]);
    if (isset($dbDate[0]) && isset($dbDate[1]) && isset($dbDate[2])) {
        return $dbDate[1] . '/' . $dbDate[2] . '/' . $dbDate[0];
    }
    return "";
}

function setImageUploadName($name) {
    $GLOBALS["imageUploadName"] = $name;
}

function getImageUploadName() {
    if (isset($GLOBALS["imageUploadName"])) {
        return $GLOBALS["imageUploadName"];
    }
    return null;
}

function adminFormAddItem($name, $data, $itemId = null) {
    $GLOBALS["admin_edit_itemData"][$name] = array($name, $data, $itemId);
}

/**
 *
 * @return array{0 => name, 1 =>data, 2 => itemId} 
 */
function getAdminFrom() {
    if (isset($GLOBALS["admin_edit_itemData"])) {
        return $GLOBALS["admin_edit_itemData"];
    }
    return null;
}

function setAdminFrom($adminFrom) {
    $GLOBALS["admin_edit_itemData"] = $adminFrom;
}

function hashByMatu($user, $pass) {
    return md5($user . $pass . 'matujetlustouckej');
}

function setCurrentCat($cat) {
    $GLOBALS["currentCat"] = $cat;
}

function getCurrentCat() {
    if (isset($GLOBALS["currentCat"])) {
        return $GLOBALS["currentCat"];
    }
    return null;
}

function setCurrentItemId($itemid) {
    $GLOBALS["currentItemId"] = $itemid;
}

function getCurrentItemId() {
    if (isset($GLOBALS["currentItemId"])) {
        return $GLOBALS["currentItemId"];
    }
    return null;
}

function addStack($methodName, $par1 = null, $par2 = null, $par3 = null, $par4 = null, $par5 = null, $par6 = null) {
    if (Settings::$debug) {
        if (!isset($GLOBALS['stackTrace'])) {
            $GLOBALS['stackTrace'] = new StackTrace();
            addStack($methodName, $par1, $par2, $par3, $par4, $par5, $par6);
        } else {
            $stack = getStackTrace();
            $stack->chain($methodName, $par1, $par2, $par3, $par4, $par5, $par6);
        }
    }
}

/**
 *
 * @return StackTrace 
 */
function getStackTrace() {
    if (isset($GLOBALS['stackTrace'])) {
        return $GLOBALS['stackTrace'];
    }
    return null;
}

function addLang($langAbstract) {
    $GLOBALS['langList'][$langAbstract::getClass()] = $langAbstract;
}

function setLang($langIdent) {
    $langs = $GLOBALS['langList'];
    foreach ($langs as $value) {
        if ($value::getClass() == $langIdent) {
            $GLOBALS['lang'] = $value;
            $GLOBALS['langName'] = $value::getClass();
            break;
        }
    }
}

function getLang() {
    return $GLOBALS['lang']->getLang();
}

function getLangName() {
    return $GLOBALS['langName'];
}

function getUserId() {
    return getUser()->getUserId();
}

/**
 *
 * @return User 
 */
function getUser() {
    return $GLOBALS["user"];
}

function isAdmin() {
    return getUser()->isAdmin();
}

function getTemplateManifest() {
    return $GLOBALS["templateManifest"];
}

function getTemplatePath($name) {
    foreach (getTemplateManifest() as $value) {
        if ($name == $value[0]) {
            return $value[1];
        }
    }
    return null;
}

function fetch($result) {
    return mysql_fetch_assoc($result);
}

?>
